package com.ydm.myapp001.service;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;

public class MyStartService extends Service {
    private static final String TAG = "YDMLog_MyStartService";

    /** 创建参数 */
    boolean threadDisable;
    int count;

    public MyStartService() {
        Log.d(TAG, "MyStartService: MyStartService()");
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Log.d(TAG, "MyStartService: onCreate()");
        /** 创建一个线程，每秒计数器加一，并在控制台进行Log输出 */
        new Thread(new Runnable() {
            public void run() {
                while (!threadDisable) { //当threadDisable设置true后，由于没有任务可执行，线程工作结束自行关闭
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {

                    }
                    count++;
                    Log.v(TAG, "Count is" + count);
                }
            }
        }).start();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.d(TAG, "MyStartService: onStartCommand()");
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onDestroy() {
        Log.d(TAG, "MyStartService: onDestroy()");
        super.onDestroy();
        /** 服务停止时，终止计数进程 */
        threadDisable = true;
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        throw new UnsupportedOperationException("Not yet implemented");
    }
}
